<?php 
namespace Home\Controller;
use Common\Controller\AdminBaseController;
class NewsController extends AdminBaseController{
	public function index(){
		$vid = I('get.vid/d');
		//一页的条数
		$totalNum = 2;
		//总条数
		 if(empty($vid)){
            //总条数
            $count = M('article')->count();
        }else{
            //总条数
            $count = M('article')->where(['cat_id'=>$vid])->count();
        }
		//总页数
		$totalPage = ceil($count/$totalNum);
		//当前页
		$page = I('get.page/d',1);
		//每一页开始的那条
		$firstC = ($page - 1) * $totalNum;

    	//循环导航栏
    	$list = M('columns')->field('link,columns')->where(['is_nav=1'])->select();
    	$lists = M('columns')->field('id,columns,pid')->where(['pid=42 || id=42'])->select();
    	if(empty($vid)){
			//读取新闻内容
			$content = M('article')->field('id,title,content,litpic')->order('id desc')->limit($firstC,$totalNum)->select();
    	}else{
    		$content = M('article')->field('dx_article.id,title,content,litpic')->join('left join dx_columns c on c.columns = dx_article.cat_id')->where(['c.id'=>$vid])->order('id desc')->limit($firstC,$totalNum)->select();
    	}
        
        foreach ($content as $key => $value) {
        	$content[$key]['img'] = rtrim($value['litpic'],',');
        }
        //循环右侧新闻列表
        $contents = M('article')->field('id,title')->limit('10')->select();
        //循环配置
		$configArr = [];
		$configInfo = M('Config') -> field('title,value,variable') -> select();
		foreach($configInfo as $key => $val){
			$configArr[$val['variable']] = $val['value'];
		}
        $this->assign('totalPage',$totalPage);
        $this->assign('count',$count);
    	$this->assign('list',$list);
    	$this->assign('lists',$lists);
    	$this->assign('contents',$contents);
    	$this->assign('content',$content);
        $this->assign('configArr',$configArr);
       	$this->display();
	}
	public function content(){
		$id=I('get.id/d');
		$vid=I('get.vid/d');
		//循环导航栏
    	$list = M('columns')->field('link,columns')->where(['is_nav=1'])->select();
    	$lists = M('columns')->field('id,columns,pid')->where(['pid=42 || id=42'])->select();
    	if(empty($vid)){
			$content = M('article')->field('*')->where(['id'=>$id])->select();
    	}else{        
       		$content = M('article')->field('id,title,content,litpic')->where(['cat_id'=>$vid])->select();
    	}
		$collection = M('collection');
		$data = $collection -> field('id,name,litpic') ->limit(5)->order('id desc')-> select();//获取藏品列表数据
		$arr1 = [];//只读取一张图片//读出藏品列表详情
		foreach ($data as $key => $val) {
			$arr1[$key]['id'] =$val['id'];
			$arr1[$key]['name'] =$val['name'];
			$arr1[$key]['pic'] =reset(explode(',',$val['litpic']));
		}
		$this ->assign('arr',$arr1);
		array_pop($arr1);
		$this ->assign('array',$arr1);
        //循环右侧新闻列表
        $contents = M('article')->field('id,title')->limit('10')->select();
        //循环配置
		$configArr = [];
		$configInfo = M('Config') -> field('title,value,variable') -> select();
		foreach($configInfo as $key => $val){
			$configArr[$val['variable']] = $val['value'];
		}
        //上一页
        $front=M('article')->field('*')->where("id<".$id)->order('id desc')->limit('1')->find();  
		$this->assign('front',$front);
        //下一页
        $after=M('article')->field('*')->where("id>".$id)->order('id')->limit('1')->find();  
		$this->assign('after',$after);
    	$this->assign('list',$list);
    	$this->assign('lists',$lists);
    	$this->assign('contents',$contents);
    	$this->assign('content',$content[0]);
        $this->assign('configArr',$configArr);
       	$this->display();
	}
}


// $front=$Article->where("id<".$id)->order('id desc')->limit('1')->find();  
// $this->assign('front',$front);  
// //下一篇  
// $after=$Article->where("id>".$id)->order('id desc')->limit('1')->find();  
// $this->assign('after',$after);


 ?>